package com.xgry.bpm.api.business.defaultfield.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgry.bpm.api.business.defaultfield.dao.DefaultFieldDao;
import com.xgry.bpm.api.business.defaultfield.entity.DefaultField;
import com.xgry.bpm.api.business.defaultfield.form.DefaultFieldForm;
import com.xgry.bpm.api.business.defaultfield.service.DefaultFieldService;
import com.xgry.bpm.api.business.defaultfield.vo.DefaultFieldVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * DefaultField服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-16
 */
@Service
        public class DefaultFieldServiceImpl extends ServiceImpl<DefaultFieldDao, DefaultField>implements DefaultFieldService {

        @Override
        public int save(DefaultFieldForm record){
            DefaultField data=this.convertDO(record);
            int insert = baseMapper.insert(data);
            record.setId(data.getId());
            return insert;
        }

        @Override
        public int updateById(DefaultFieldForm record){
        //修改时间
    DefaultField data=this.convertDO(record);
        return baseMapper.updateById(data);
        }

        @Override
        public int deleteById(String id){
        return baseMapper.deleteById(id);
        }

        @Override
        public DefaultFieldVO selectById(String id){
        DefaultField data=baseMapper.selectById(id);
        DefaultFieldVO vo=new DefaultFieldVO();
        BeanUtils.copyProperties(data,vo);
        return vo;
        }

        @Override
        public List<DefaultFieldVO>selectAll(DefaultFieldForm record){
    DefaultField data=this.convertDO(record);
        QueryWrapper<DefaultField>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        List<DefaultField>list=baseMapper.selectList(queryWrapper);
        return this.convert(list);
        }

        @Override
        public IPage<DefaultFieldVO>selectPage(DefaultFieldForm record){
        // form -> do 转换
    DefaultField data=this.convertDO(record);
        // 分页数据设置
        Page<DefaultField>page=new Page<>(record.getCurrent(),record.getSize());
        // 查询条件
        QueryWrapper<DefaultField>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(data);
        IPage<DefaultField>list=baseMapper.selectPage(page,queryWrapper);
        IPage<DefaultFieldVO>iPage=new Page<>();
        iPage.setRecords(this.convert(list.getRecords()));
        iPage.setCurrent(list.getCurrent());
        iPage.setSize(list.getSize());
        iPage.setTotal(list.getTotal());
        iPage.setPages(list.getPages());
        return iPage;
        }

        @Override
        public IPage<DefaultField>getPage(IPage<DefaultField>page,QueryWrapper<DefaultField>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
        }


        /**
        * Form -> Do
        * @param form 对象
        * @return Do对象
        */
        private DefaultField convertDO(DefaultFieldForm form){
            DefaultField data=new DefaultField();
            BeanUtils.copyProperties(form,data);
        return data;
        }

        /**
        * Do -> VO
        * @param list 对象
        * @return VO对象
        */
        private List<DefaultFieldVO>convert(List<DefaultField>list){
        List<DefaultFieldVO> defaultFieldList=new ArrayList<>();
        if(CollectionUtils.isEmpty(list)){
        return defaultFieldList;
        }
        for(DefaultField source:list){
    DefaultFieldVO target=new DefaultFieldVO();
        BeanUtils.copyProperties(source,target);
    defaultFieldList.add(target);
        }
        return defaultFieldList;
        }

        }
